package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.UserAddress;
import com.ruoyi.system.domain.vo.AddressVo;
import com.ruoyi.system.domain.vo.RegisterVo;
import com.ruoyi.system.domain.vo.UserInfo;
import com.ruoyi.system.mapper.UserAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.UserMapper;
import com.ruoyi.system.domain.User;
import com.ruoyi.system.service.IUserService;

/**
 * 用户登录Service业务层处理
 * 
 * @author Wxl
 * @date 2022-03-26
 */
@Service
public class UserServiceImpl implements IUserService 
{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    /**
     * 查询用户登录
     * 
     * @param id 用户登录主键
     * @return 用户登录
     */
    @Override
    public User selectUserById(Long id)
    {
        return userMapper.selectUserById(id);
    }

    /**
     * 查询用户登录列表
     * 
     * @param user 用户登录
     * @return 用户登录
     */
    @Override
    public List<User> selectUserList(User user)
    {
        return userMapper.selectUserList(user);
    }

    /**
     * 新增用户登录
     * 
     * @param user 用户登录
     * @return 结果
     */
    @Override
    public int insertUser(User user)
    {
        user.setCreateTime(DateUtils.getNowDate());
        return userMapper.insertUser(user);
    }

    /**
     * 修改用户登录
     * 
     * @param user 用户登录
     * @return 结果
     */
    @Override
    public int updateUser(User user)
    {
        return userMapper.updateUser(user);
    }

    /**
     * 批量删除用户登录
     * 
     * @param ids 需要删除的用户登录主键
     * @return 结果
     */
    @Override
    public int deleteUserByIds(Long[] ids)
    {
        return userMapper.deleteUserByIds(ids);
    }

    /**
     * 删除用户登录信息
     * 
     * @param id 用户登录主键
     * @return 结果
     */
    @Override
    public int deleteUserById(Long id)
    {
        return userMapper.deleteUserById(id);
    }

    @Override
    public Long register(RegisterVo register,String  openId) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("open_id", openId));
        if (user!=null){
            return user.getId();
        }
        User newUser = new User();
        long id = RandomUtil.randomLong(9999999999L);
        newUser.setId(id);
        newUser.setUsername(register.getUserInfo().getNickName());
        newUser.setGender(register.getUserInfo().getGender());
        newUser.setOpenId(openId);
        newUser.setAvatar(register.getUserInfo().getAvatarUrl());
        int i = userMapper.insert(newUser);
        return id;
    }

    @Override
    public List<AddressVo> address(Long userId) {
        List<UserAddress> address = userAddressMapper.selectList(new QueryWrapper<UserAddress>().eq("user_id", userId));
        List<AddressVo> addressVoList = new ArrayList<AddressVo>();
        for (UserAddress userAddress : address) {
            AddressVo addressVo = new AddressVo();
            addressVo.setId(userAddress.getId());
            addressVo.setDefaultFlag(userAddress.getDefaultFlag());
            addressVo.setAddressName("石家庄学院");
            addressVo.setAddress(userAddress.getAddress());
            addressVo.setArea(userAddress.getArea());
            addressVo.setName(userAddress.getConsignee());
            addressVo.setMobile(userAddress.getMobile());
            addressVoList.add(addressVo);
        }
        return addressVoList;
    }
}
